package com.dhcc.sds.standard.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.dhcc.cdc.busi.dao.*;

import com.dhcc.cdc.busi.entity.*;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.standard.dao.SdsStandMatchLogMapper;
import com.dhcc.sds.standard.dto.CdcExamdicDto;

import com.dhcc.sds.standard.dto.CdcOrdmastDto;
import com.dhcc.sds.standard.entity.*;
import com.dhcc.sds.standard.service.ISdsStandDicMatchParamService;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import com.dhcc.sds.standard.service.ISdsStandDicsubService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sds.standard.dao.SdsStandRisitemMatchMapper;
import com.dhcc.sds.standard.service.ISdsStandRisitemMatchService;
import org.springframework.util.StringUtils;

/**
 * 标准化_检查项目配置表数据服务层接口
 * 
 * @ClassName: SdsStandRisitemMatchServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:53
 */
@Service("sdsStandRisitemMatchService")
public class SdsStandRisitemMatchServiceImpl extends BaseServiceImpl<SdsStandRisitemMatchMapper, SdsStandRisitemMatchEntity> implements ISdsStandRisitemMatchService {

	@Autowired
	private CdcExamitemMapper cdcExamitemMapper;

	@Autowired
	private ISdsStandDicService sdsStandDicService;

	@Autowired
	private ISdsStandDicsubService sdsStandDicsubService;

	@Autowired
	private CdcOrdmastMapper ordmastMapper;

	@Autowired
	private CdcExamdicMapper cdcExamdicMapper;

	@Autowired
	private CdcOrdexecMapper cdcOrdexecMapper;

	@Autowired
	private CdcExaminateMapper cdcExaminateMapper;

	@Autowired
	private CdcDictionaryMapper cdcDictionaryMapper;

	@Autowired
	private SdsStandMatchLogMapper sdsStandMatchLogMapper;

	@Autowired
	private IDictionaryService dictionaryService;

	@Autowired
	private ICdcDictionaryService cdcDictionaryService;

	@Autowired
	private ISdsStandDicMatchParamService sdsStandDicMatchParamService;


	@Override
	public List<SdsStandRisitemMatchEntity> page(Page<SdsStandRisitemMatchEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdsStandRisitemMatchEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public void compare(Long sdsStandDicsubId, String risitemIds, Long sdsStandDicId, String risorderIds, String username) {
		/*if (!StringUtils.isEmpty(sdsStandDicsubId)){
			//标准字典子项关联检查项目
			String[] split1 = risitemIds.split(",");
			for (int i = 0; i < split1.length; i++){
				if (split1[i] != null || !"".equals(split1[i])){
					SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity = new SdsStandRisitemMatchEntity();
					sdsStandRisitemMatchEntity.setExamdicId(Long.parseLong(split1[i]));
					sdsStandRisitemMatchEntity.setDicsubId(sdsStandDicsubId);
					SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity1 = this.baseMapper.selectOne(sdsStandRisitemMatchEntity);
					if (sdsStandRisitemMatchEntity1 == null){
						//insert
						this.baseMapper.insert(sdsStandRisitemMatchEntity);
					}
				}
			}
		}else {
			//标准字典关联检查医嘱
			String[] split1 = risorderIds.split(",");
			for (int i = 0; i < split1.length; i++){
				if (split1[i] != null || !"".equals(split1[i])){
					SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity = new SdsStandRisitemMatchEntity();
					sdsStandRisitemMatchEntity.setOrdmastId(Long.parseLong(split1[i]));
					sdsStandRisitemMatchEntity.setDicId(sdsStandDicId);
					SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity1 = this.baseMapper.selectOne(sdsStandRisitemMatchEntity);
					if (sdsStandRisitemMatchEntity1 == null){
						//insert
						this.baseMapper.insert(sdsStandRisitemMatchEntity);
					}
				}
			}
		}*/

		String[] split1 = risorderIds.split(",");
		String[] split2 = risitemIds.split(",");
		if (!StringUtils.isEmpty(sdsStandDicsubId)){
			for (String risitemId : split2){
				SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity = new SdsStandRisitemMatchEntity();
				SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();
				sdsStandRisitemMatchEntity.setDicsubId(sdsStandDicsubId);
				sdsStandRisitemMatchEntity.setExamdicId(Long.valueOf(risitemId));
				sdsStandRisitemMatchEntity.setMatchType("H");

				sdsStandMatchLogEntity.setDicsubId(sdsStandDicsubId);
				sdsStandMatchLogEntity.setExamdicId(Long.valueOf(risitemId));

				sdsStandMatchLogEntity.setOperType(1);
				sdsStandMatchLogEntity.setMatchType("检查对照");
				sdsStandMatchLogEntity.setOperUser(username);
				SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity1 = this.baseMapper.selectOne(sdsStandRisitemMatchEntity);
				if (sdsStandRisitemMatchEntity1 == null){
					//insert
					this.baseMapper.insert(sdsStandRisitemMatchEntity);
					sdsStandMatchLogEntity.setOperTime(new Date());
					//更新日志
					sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
				}
			}
		}else {
			for (String risorderId : split1){
				SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity = new SdsStandRisitemMatchEntity();
				SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();
				sdsStandRisitemMatchEntity.setDicId(sdsStandDicId);
				sdsStandRisitemMatchEntity.setOrdmastId(Long.valueOf(risorderId));
				sdsStandRisitemMatchEntity.setMatchType("H");

				sdsStandMatchLogEntity.setDicId(sdsStandDicId);
				sdsStandMatchLogEntity.setOrdmastId(Long.valueOf(risorderId));
				sdsStandMatchLogEntity.setOperType(1);
				sdsStandMatchLogEntity.setMatchType("检查对照");
				sdsStandMatchLogEntity.setOperUser(username);
				SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity1 = this.baseMapper.selectOne(sdsStandRisitemMatchEntity);
				if (sdsStandRisitemMatchEntity1 == null){
					//insert
					this.baseMapper.insert(sdsStandRisitemMatchEntity);
					sdsStandMatchLogEntity.setOperTime(new Date());
					//更新日志
					sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
				}
			}

		}


	}

	@Override
	public void undo(String risitemIds, Long sdsStandDicsubId, String risorderIds, Long sdsStandDicId, String username) {

		String[] split1 = risorderIds.split(",");
		String[] split2 = risitemIds.split(",");


		if (!StringUtils.isEmpty(sdsStandDicsubId)){
			for (String risitemId : split2){
				SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity = new SdsStandRisitemMatchEntity();
				SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();
				sdsStandRisitemMatchEntity.setDicsubId(sdsStandDicsubId);
				sdsStandRisitemMatchEntity.setExamdicId(Long.valueOf(risitemId));

				sdsStandMatchLogEntity.setDicsubId(sdsStandDicsubId);
				sdsStandMatchLogEntity.setExamdicId(Long.valueOf(risitemId));
				sdsStandMatchLogEntity.setOperType(2);
				sdsStandMatchLogEntity.setMatchType("检查对照");
				sdsStandMatchLogEntity.setOperUser(username);
				SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity1 = this.baseMapper.selectOne(sdsStandRisitemMatchEntity);
				this.baseMapper.deleteById(sdsStandRisitemMatchEntity1);
				sdsStandMatchLogEntity.setOperTime(new Date());
				sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
			}
		}else {
			for (String risorderId : split1){
				SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity = new SdsStandRisitemMatchEntity();
				SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();
				sdsStandRisitemMatchEntity.setDicId(sdsStandDicId);
				sdsStandRisitemMatchEntity.setOrdmastId(Long.valueOf(risorderId));

				sdsStandMatchLogEntity.setDicsubId(sdsStandDicsubId);
				sdsStandMatchLogEntity.setOrdmastId(Long.valueOf(risorderId));
				sdsStandMatchLogEntity.setOperType(2);
				sdsStandMatchLogEntity.setMatchType("检查对照");
				sdsStandMatchLogEntity.setOperUser(username);
				SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity1 = this.baseMapper.selectOne(sdsStandRisitemMatchEntity);
				this.baseMapper.deleteById(sdsStandRisitemMatchEntity1);
				sdsStandMatchLogEntity.setOperTime(new Date());
				sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
			}
		}

	}

	@Override
	public void autoCompareRisitem() {
		//查询检查项目数据
		//List<CdcExamitemEntity> cdcExamitemEntities = cdcExamitemMapper.selectList(null);
		List<CdcExamdicEntity> cdcExamdicEntities = cdcExamdicMapper.selectList(null);

		//查询标准检查项目数据
		List<SdsStandDicEntity> sdsStandDicEntities = sdsStandDicService.listBySource("检查");
		List<SdsStandDicsubEntity> standDicsubEntities = sdsStandDicsubService.listByDic(sdsStandDicEntities);

		for (SdsStandDicsubEntity dicsubEntity : standDicsubEntities){
			for (CdcExamdicEntity cdcExamdicEntity : cdcExamdicEntities){
				//自动匹配：检查代码匹配
				if (dicsubEntity.getDicCode().equals(cdcExamdicEntity.getCode())){
					SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity = new SdsStandRisitemMatchEntity();
					sdsStandRisitemMatchEntity.setExamdicId(cdcExamdicEntity.getId());
					sdsStandRisitemMatchEntity.setDicsubId(dicsubEntity.getId());
					SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity1 = this.baseMapper.selectOne(sdsStandRisitemMatchEntity);
					if (sdsStandRisitemMatchEntity1 == null){
						//不存在insert
						this.baseMapper.insert(sdsStandRisitemMatchEntity);
					}
				}
			}
		}
	}

	@Override
	public List<CdcExamdicDto> pageExamdic(Page<CdcExamdicEntity> page, Map<String, Object> params) {
		Object dicSubId = params.get("dicSubId");
		Object dicId = params.get("dicId");
		String compare = (String) params.get("compare");
		String s = String.valueOf(dicSubId);
		String s2 = String.valueOf(dicId);
		List<CdcExamdicDto> cdcExamdicDtoList = CollUtil.newArrayList();
		if ("0".equals(s)){
			return  cdcExamdicDtoList;
		}

		//通过医嘱id查询医嘱执行记录->检查报告表->检查项目表->检查项目字典表
		List<Long> ordmastIds = new ArrayList<>();
		List<Long> ordexecIds = new ArrayList<>();
		List<Long> examinateIds = new ArrayList<>();
		List<Long> examdicIds = new ArrayList<>();
		if (!StringUtils.isEmpty(s2)){
			//关联检查医嘱
			 List<SdsStandRisitemMatchEntity> risitemMatchEntities = this.baseMapper.selectList(
					 new EntityWrapper<SdsStandRisitemMatchEntity>()
							 .eq("dic_id", s2)
			 );
			 for (SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity : risitemMatchEntities){
				 Long ordmastId = sdsStandRisitemMatchEntity.getOrdmastId();
				 ordmastIds.add(ordmastId);
			 }

			if (null == ordmastIds || ordmastIds.size() ==0){
				return cdcExamdicDtoList;
			}

			//根据医嘱id查询符合的检查项目字典表id
            Map<String, Object> map = new HashMap<>();
            map.put("dicsubId", s);
            List<Long> matchExamdicIds = this.baseMapper.selectMatchExamdicIds(map);
            List<CdcOrdexecEntity> cdcLabEntryEntities = cdcOrdexecMapper.selectByOrdmastIds(ordmastIds, null);
            for (CdcOrdexecEntity cdcOrdexecEntity : cdcLabEntryEntities){
                Long id = cdcOrdexecEntity.getId();
                ordexecIds.add(id);
            }

            if (null == ordexecIds || ordexecIds.size() ==0){
                return cdcExamdicDtoList;
            }

            List<CdcExaminateEntity> cdcExaminateEntities = cdcExaminateMapper.selectByOrdexecIds(ordexecIds);
            for (CdcExaminateEntity cdcExaminateEntity : cdcExaminateEntities){
                Long id = cdcExaminateEntity.getId();
                examinateIds.add(id);
            }

            if (null == examinateIds || examinateIds.size() ==0){
                return cdcExamdicDtoList;
            }

            List<CdcExamitemEntity> cdcExamitemEntities = cdcExamitemMapper.selectByExaminateIds(examinateIds);
            for (CdcExamitemEntity cdcExamitemEntity : cdcExamitemEntities){
                Long examdicId = cdcExamitemEntity.getExamdicId();
                examdicIds.add(examdicId);
            }

            List<CdcExamdicEntity> cdcExamdicEntities = new ArrayList<>();
            if (Objects.equals("all", compare)){
                cdcExamdicEntities = cdcExamdicMapper.selectByExamdicIds(page, examdicIds);
            }else {
                //差集
                examdicIds.removeAll(matchExamdicIds);
                cdcExamdicEntities = cdcExamdicMapper.selectByExamdicIds(page, examdicIds);
            }

            for (CdcExamdicEntity cdcExamdicEntity : cdcExamdicEntities){
                CdcExamdicDto cdcExamdicDto = new CdcExamdicDto();
                BeanUtil.copyProperties(cdcExamdicEntity,cdcExamdicDto);
                cdcExamdicDtoList.add(cdcExamdicDto);
            }

            List<SdsStandRisitemMatchEntity> sdsStandRisitemMatchEntityList = this.baseMapper.selectList(
                    new EntityWrapper<SdsStandRisitemMatchEntity>()
                            .eq("dicsub_id", s)
            );
            //关联的数据进行勾选
            for (CdcExamdicDto cdcExamdicDto : cdcExamdicDtoList){
                for (SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity : sdsStandRisitemMatchEntityList){
                    if (cdcExamdicDto.getId().equals(sdsStandRisitemMatchEntity.getExamdicId())){
                        cdcExamdicDto.setIsCheck(true);
                    }
                }
            }

			/*List<CdcOrdexecEntity> cdcLabEntryEntities = cdcOrdexecMapper.selectByOrdmastIds(ordmastIds);
			for (CdcOrdexecEntity cdcOrdexecEntity : cdcLabEntryEntities){
				Long id = cdcOrdexecEntity.getId();
				ordexecIds.add(id);
			}

			if (null == ordexecIds || ordexecIds.size() ==0){
				return cdcExamdicDtoList;
			}

			List<CdcExaminateEntity> cdcExaminateEntities = cdcExaminateMapper.selectByOrdexecIds(ordexecIds);
			for (CdcExaminateEntity cdcExaminateEntity : cdcExaminateEntities){
				Long id = cdcExaminateEntity.getId();
				examinateIds.add(id);
			}

			if (null == examinateIds || examinateIds.size() ==0){
				return cdcExamdicDtoList;
			}

			List<CdcExamitemEntity> cdcExamitemEntities = cdcExamitemMapper.selectByExaminateIds(examinateIds);
			for (CdcExamitemEntity cdcExamitemEntity : cdcExamitemEntities){
				Long examdicId = cdcExamitemEntity.getExamdicId();
				examdicIds.add(examdicId);
			}

			if (null == examdicIds || examdicIds.size() ==0){
				return cdcExamdicDtoList;
			}

			List<CdcExamdicEntity> cdcExamdicEntities = new ArrayList<>();
			Map<String, Object> map = new HashMap<>();
			if (Objects.equals("all", compare)){
				//全部
				cdcExamdicEntities = cdcExamdicMapper.selectByExamdicIds(page, examdicIds);
			}else {
				//未对照
				map.put("dicsubId", s);
				List<Long> matchExamdicIds = this.baseMapper.selectMatchExamdicIds(map);
				cdcExamdicEntities = cdcExamdicMapper.pageExcludeMatch(page, params, matchExamdicIds);
			}


			for (CdcExamdicEntity cdcExamdicEntity : cdcExamdicEntities){
				CdcExamdicDto cdcExamdicDto = new CdcExamdicDto();
				BeanUtil.copyProperties(cdcExamdicEntity,cdcExamdicDto);
				cdcExamdicDtoList.add(cdcExamdicDto);
			}

            List<SdsStandRisitemMatchEntity> sdsStandRisitemMatchEntityList = this.baseMapper.selectList(
                    new EntityWrapper<SdsStandRisitemMatchEntity>()
                            .eq("dicsub_id", s)
            );
            //关联的数据进行勾选
            for (CdcExamdicDto cdcExamdicDto : cdcExamdicDtoList){
                for (SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity : sdsStandRisitemMatchEntityList){
                    if (cdcExamdicDto.getId().equals(sdsStandRisitemMatchEntity.getExamdicId())){
                        cdcExamdicDto.setIsCheck(true);
                    }
                }
            }*/

		}

		return cdcExamdicDtoList;

		//查询检查项目字典表所有数据
		/*List<CdcExamdicEntity> page1 = cdcExamdicMapper.page(page, params);
		for (CdcExamdicEntity cdcExamdicEntity : page1){
			CdcExamdicDto cdcExamdicDto = new CdcExamdicDto();
			BeanUtil.copyProperties(cdcExamdicEntity,cdcExamdicDto);
			cdcExamdicDtoList.add(cdcExamdicDto);
		}*/

		/*if ((dicSubId != null) && (!"".equals(dicSubId))){
			//点击标准化字典子项
			List<SdsStandRisitemMatchEntity> sdsStandRisitemMatchEntityList = this.baseMapper.selectList(
					new EntityWrapper<SdsStandRisitemMatchEntity>()
							.eq("dicsub_id", s)
			);
			//关联的数据进行勾选
			for (CdcExamdicDto cdcExamdicDto : cdcExamdicDtoList){
				for (SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity : sdsStandRisitemMatchEntityList){
					if (cdcExamdicDto.getId().equals(sdsStandRisitemMatchEntity.getExamdicId())){
						cdcExamdicDto.setIsCheck(true);
					}
				}
			}

			Object o = params.get("compare");
			String compare = String.valueOf(o);
			//已对照-yes，未对照-no，全部
			List<CdcExamdicDto> collect = new ArrayList<>();
			if("yes".equals(compare)){
				collect = cdcExamdicDtoList.stream().filter(item -> (item.getIsCheck() != null && item.getIsCheck() == true)).collect(Collectors.toList());
				return collect;
			}else if("no".equals(compare)){
				collect = cdcExamdicDtoList.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
				return collect;
			}else {
				return cdcExamdicDtoList;
			}
		}else {
			//不点击默认查询所有
			return cdcExamdicDtoList;
		}*/
	}

	@Override
	public void autoCompareRisorder() {
		//检查目前只对照标准字典和检查医嘱
		List<SdsStandDicMatchParamEntity> standDicMatchEntities = sdsStandDicMatchParamService.getDicBySource(Arrays.asList("影像检查"));
		CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.queryByTypeCodeAndCode("OrdData", "bus_order_cate", "检查");
		List<CdcOrdmastEntity> cdcOrdmastEntities = ordmastMapper.selectList(new EntityWrapper<CdcOrdmastEntity>()
				.eq("bus_order_cate_id", cdcDictionaryEntity.getId())
				.eq("is_active", 1));
		List<CdcOrdmastDto> cdcOrdmastDtos = cdcOrdmastEntities.stream().map(t -> {
			CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
			BeanUtils.copyProperties(t, cdcOrdmastDto);
			return cdcOrdmastDto;
		}).collect(Collectors.toList());

		Map<Long, List<SdsStandDicMatchParamEntity>> dicMatchMap = new HashMap<>();
		//处理dic数据
		for (SdsStandDicMatchParamEntity sdsStandDicMatchParamEntity : standDicMatchEntities){
			if (CommonUtil.isEmpty(dicMatchMap.get(sdsStandDicMatchParamEntity.getDicId()))){
				List<SdsStandDicMatchParamEntity> dicMatchEntities = new ArrayList<>();
				dicMatchEntities.add(sdsStandDicMatchParamEntity);
				dicMatchMap.put(sdsStandDicMatchParamEntity.getDicId(), dicMatchEntities);
			}else {
				List<SdsStandDicMatchParamEntity> dicMatchEntities = dicMatchMap.get(sdsStandDicMatchParamEntity.getDicId());
				dicMatchEntities.add(sdsStandDicMatchParamEntity);
			}
		}

		List<SdsStandRisitemMatchEntity> standRisitemMatchEntities = new ArrayList<>();

		Map<Long, List<CdcOrdmastDto>> ordmastMap = new HashMap<>();
		for (Long dicId : dicMatchMap.keySet()){
			List<SdsStandDicMatchParamEntity> dicMatchEntities = dicMatchMap.get(dicId);
			String filterTxt = "";
			List<CdcOrdmastDto> matchOrdmast = new ArrayList<>();
			a:for (SdsStandDicMatchParamEntity standDicMatchEntity : dicMatchEntities){
				String filterType = standDicMatchEntity.getFilterType();
				String matchTxt = standDicMatchEntity.getCompareContent();
				if (Objects.equals("O", filterType)){
					filterTxt = filterTxt + ":" + matchTxt + ",";
					continue a;
				}
				for (CdcOrdmastDto cdcOrdmastDto : cdcOrdmastDtos){
					String drugCommonName = cdcOrdmastDto.getDrugCommonName();
					String description = cdcOrdmastDto.getDescription();
					////处理并且关系的关键字:&分隔的关键字同时满足
					boolean checkFlg = true;
					if (matchTxt.contains("&")){
						String[] split = matchTxt.split("&");
						b:for (String s : split){
							if (StringUtils.isEmpty(drugCommonName)){
								if (!description.contains(s)){
									checkFlg = false;
									break b;
								}
							}else {
								if (!description.contains(s) && !drugCommonName.contains(s)){
									checkFlg = false;
									break b;
								}
							}
						}
						if (checkFlg){
							cdcOrdmastDto.setMatchType("K");
							matchOrdmast.add(cdcOrdmastDto);
							ordmastMap.put(dicId, matchOrdmast);
						}
					}else {
						if (StringUtils.isEmpty(drugCommonName)){
							if (description.contains(matchTxt)){
								cdcOrdmastDto.setMatchType("K");
								matchOrdmast.add(cdcOrdmastDto);
								ordmastMap.put(dicId, matchOrdmast);
							}
						}else {
							if (description.contains(matchTxt) || drugCommonName.contains(matchTxt)){
								cdcOrdmastDto.setMatchType("K");
								matchOrdmast.add(cdcOrdmastDto);
								ordmastMap.put(dicId, matchOrdmast);
							}
						}
					}
				}
			}
			//排除
			if (!StringUtils.isEmpty(filterTxt)){
				String[] split = filterTxt.split(",");
				List<CdcOrdmastDto> ordmastEntities = ordmastMap.get(dicId);
				Iterator<CdcOrdmastDto> it = ordmastEntities.iterator();
				while (it.hasNext()){
					CdcOrdmastDto ordmastEntity = it.next();
					for (String s : split){
						String[] split1 = s.split(":");
						if (StringUtils.isEmpty(ordmastEntity.getDrugCommonName())){
							if (ordmastEntity.getDescription().contains(split1[1])){
								it.remove();
							}
						}else {
							if (ordmastEntity.getDescription().contains(split1[1]) || ordmastEntity.getDrugCommonName().contains(split1[1])){
								it.remove();
							}
						}
					}
				}
				ordmastMap.put(dicId, ordmastEntities);
			}

		}

		for (Long dicId : ordmastMap.keySet()){
			List<CdcOrdmastDto> ordmastEntities = ordmastMap.get(dicId);
			for (CdcOrdmastDto cdcOrdmastEntity : ordmastEntities){
				SdsStandRisitemMatchEntity standRisitemMatchEntity = new SdsStandRisitemMatchEntity();
				standRisitemMatchEntity.setDicId(dicId);
				standRisitemMatchEntity.setOrdmastId(cdcOrdmastEntity.getId());
				SdsStandRisitemMatchEntity standRisitemMatchEntity1 = this.baseMapper.selectOne(standRisitemMatchEntity);
				if (StringUtils.isEmpty(standRisitemMatchEntity1)) {
					standRisitemMatchEntity.setMatchType(cdcOrdmastEntity.getMatchType());
					standRisitemMatchEntities.add(standRisitemMatchEntity);
				}
			}
		}

		this.baseMapper.insertBatch(standRisitemMatchEntities);
	}

	@Override
	public List<CdcOrdmastDto> pageOrdmast(Page<CdcOrdmastEntity> page, Map<String, Object> params) {
		Object dicId = params.get("dicId");
		Object dicSubId = params.get("dicSubId");
		String compare = (String) params.get("compare");
		String s = String.valueOf(dicId);
		if ("0".equals(s)){
			return null;
		}

		//过滤医嘱为检查的
		//String typeCode = dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "Check");
		//CdcDictionaryEntity dictionaryEntity = cdcDictionaryService.queryByTypeCodeAndCode("OrdData", "bus_order_cate", typeCode);
		CdcDictionaryEntity dictionaryEntity = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "Check");
		if (!StringUtils.isEmpty(dictionaryEntity)){
			params.put("busOrderCateId",dictionaryEntity.getId());
		}

		List<CdcOrdmastEntity> page1 = new ArrayList<>();
		List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
		List<SdsStandRisitemMatchEntity> sdsStandOrderMatchEntities = new ArrayList<>();
		if (!StringUtils.isEmpty(dicSubId)){
			String s1 = String.valueOf(dicSubId);
			params.put("dicsubId", s1);
			sdsStandOrderMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandRisitemMatchEntity>()
							.eq("dicsub_id", s)
			);
		}else {
			String s1 = String.valueOf(dicId);
			params.put("dicId", s1);
			sdsStandOrderMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandRisitemMatchEntity>()
							.eq("dic_id", s)
			);
		}

		if (Objects.equals("all", compare)){
			page1 = ordmastMapper.page(page, params);
		}else {
			List<Long> matchOrdmastIds = this.baseMapper.selectMatchOrdmastIds(params);
			page1 = ordmastMapper.pageExcludeMatch(page, params, matchOrdmastIds);
		}

		for (CdcOrdmastEntity cdcOrdmastEntity : page1){
			CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
			BeanUtil.copyProperties(cdcOrdmastEntity, cdcOrdmastDto);
			CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryMapper.selectById(cdcOrdmastDto.getBusOrderTypeId());
			cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
			cdcOrdmastDtoList.add(cdcOrdmastDto);
		}

		//关联的数据进行勾选
		for (CdcOrdmastDto cdcOrdmastDto : cdcOrdmastDtoList){
			for (SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity : sdsStandOrderMatchEntities){
				if (cdcOrdmastDto.getId().equals(sdsStandRisitemMatchEntity.getOrdmastId())){
					cdcOrdmastDto.setIsCheck(true);
				}
			}
		}

		return cdcOrdmastDtoList;

		/*List<CdcOrdmastEntity> page1 = ordmastMapper.page(page, params);
		List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
		for (CdcOrdmastEntity cdcOrdmastEntity : page1){
			CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
			BeanUtil.copyProperties(cdcOrdmastEntity, cdcOrdmastDto);
			CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryMapper.selectById(cdcOrdmastDto.getBusOrderTypeId());
			cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
			cdcOrdmastDtoList.add(cdcOrdmastDto);
		}

		if ((s != null) && (!"".equals(s))){
			//点击标准化字典
			List<SdsStandRisitemMatchEntity> risitemMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandRisitemMatchEntity>()
							.eq("dic_id", s)
			);
			//关联的数据进行勾选
			for (CdcOrdmastDto cdcOrdmastDto : cdcOrdmastDtoList){
				for (SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity : risitemMatchEntities){
					if (cdcOrdmastDto.getId().equals(sdsStandRisitemMatchEntity.getOrdmastId())){
						cdcOrdmastDto.setIsCheck(true);
					}
				}
			}
		}
		//已对照-yes，未对照-no，全部
		Object o = params.get("compare");
		String compare = String.valueOf(o);
		List<CdcOrdmastDto> collect = new ArrayList<>();
		if("yes".equals(compare)){
			collect = cdcOrdmastDtoList.stream().filter(item -> (item.getIsCheck() != null && item.getIsCheck() == true)).collect(Collectors.toList());
			return collect;
		}else if("no".equals(compare)){
			collect = cdcOrdmastDtoList.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
			return collect;
		}else {
			return cdcOrdmastDtoList;
		}*/
	}

	@Override
	public List<CdcOrdmastDto> pageMatchOrdmast(Page<SdsStandRisitemMatchEntity> page, Map<String, Object> params) {
		Object dicId = params.get("dicId");
		Object dicSubId = params.get("dicSubId");
		if (StringUtils.isEmpty(dicSubId) && StringUtils.isEmpty(dicId)){
			return null;
		}

		List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
		if (!StringUtils.isEmpty(dicSubId)){
			//点击标准化字典子项
			String s = String.valueOf(dicSubId);
			params.put("dicsubId", s);
		}else if (!StringUtils.isEmpty(dicId)){
			String s = String.valueOf(dicId);
			params.put("dicId", s);
		}
		List<SdsStandRisitemMatchEntity> page1 = this.baseMapper.page(page, params);
		//默认查询已对照医嘱，未对照返回空
		if (page1.size() != 0){
			for (SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity : page1){
				CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
				CdcOrdmastEntity ordmastEntity = ordmastMapper.selectById(sdsStandRisitemMatchEntity.getOrdmastId());
				BeanUtil.copyProperties(ordmastEntity, cdcOrdmastDto);
				//医嘱项busOrderTypeId为空情况
				if (!StringUtils.isEmpty(cdcOrdmastDto.getBusOrderTypeId())){
					CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryMapper.selectById(cdcOrdmastDto.getBusOrderTypeId());
					cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
				}
				cdcOrdmastDto.setIsCheck(true);
				cdcOrdmastDtoList.add(cdcOrdmastDto);
			}
		}
		return cdcOrdmastDtoList;
	}

	@Override
	public List<CdcExamdicDto> pageMatchExamdic(Page<SdsStandRisitemMatchEntity> page, Map<String, Object> params) {
		Object dicSubId = params.get("dicSubId");
		if (StringUtils.isEmpty(dicSubId)){
			return null;
		}
		List<CdcExamdicDto> cdcExamdicDtos = CollUtil.newArrayList();
		String s = String.valueOf(dicSubId);
		params.put("dicId", "");
		params.put("dicsubId", s);
		List<SdsStandRisitemMatchEntity> page1 = this.baseMapper.page(page, params);
		//默认查询已对照医嘱，未对照返回空
		if (page1.size() != 0){
			for (SdsStandRisitemMatchEntity sdsStandRisitemMatchEntity : page1){
				CdcExamdicDto cdcExamdicDto = new CdcExamdicDto();
				CdcExamdicEntity cdcExamdicEntity = cdcExamdicMapper.selectById(sdsStandRisitemMatchEntity.getExamdicId());
				BeanUtil.copyProperties(cdcExamdicEntity, cdcExamdicDto);
				cdcExamdicDto.setIsCheck(true);
				cdcExamdicDtos.add(cdcExamdicDto);
			}
		}

		return cdcExamdicDtos;
	}

	@Override
	public List<Long> selectMatchDicsub() {
		return this.baseMapper.selectMatchDicsub();
	}

}